home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / RAND_KEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  2.9 KB  |  134 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/random_key.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>.
  9.  *
  10.  * These routines perform encryption and decryption using the DES
  11.  * private key algorithm, or else a subset of it-- fewer inner loops.
  12.  * ( AUTH_DES_ITER defaults to 16, may be less)
  13.  *
  14.  * Under U.S. law, this software may not be exported outside the US
  15.  * without license from the U.S. Commerce department.
  16.  *
  17.  * The key schedule is passed as an arg, as well as the cleartext or
  18.  * ciphertext.     The cleartext and ciphertext should be in host order.
  19.  *
  20.  * These routines form the library interface to the des facilities.
  21.  *
  22.  * spm    8/85    MIT project athena
  23.  */
  24.  
  25. #ifndef    lint
  26. static char rcsid_random_key_c[] =
  27. "$Header: random_key.c,v 4.8 89/01/21 16:50:39 jtkohl Exp $";
  28. #endif    lint
  29.  
  30. #include <mit_copy.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33.  
  34. #include <des.h>
  35. #include "des_intn.h"
  36.  
  37. #ifdef BSDUNIX
  38. #include <sys/time.h>
  39. #endif
  40.  
  41. extern int des_debug;
  42. extern int des_debug_print();
  43.  
  44. /* random_key */
  45. int
  46. des_random_key(key)
  47.     des_cblock *key;
  48. {
  49.     /*
  50.      * create a random des key; should force odd parity per byte;
  51.      * parity is bits 8,16,...64 in des order, implies 0, 8, 16, ...
  52.      * vax order
  53.      */
  54.  
  55.     register unsigned int temp;
  56.     register int odd;
  57.     register unsigned char *c = (unsigned char *) key;
  58.     unsigned long *k = (unsigned long *) key;
  59.     static long p = 0;
  60.     static long n = 0;
  61.     long gethostid();
  62.  
  63.     int i,j;
  64.  
  65. #ifdef BSDUNIX 
  66.  
  67.     static struct timeval time;
  68.  
  69.     if (!p) {
  70.     p = getpid();
  71.     p ^= gethostid();
  72.     }
  73.  
  74.     (void) gettimeofday(&time,(struct timezone *)0);
  75.     /* randomize start */
  76.     srand(time.tv_usec ^ time.tv_sec ^ p ^ n++);
  77.  
  78.     *k++ = rand();
  79.     *k = rand();
  80.  
  81.     /* make each byte parity odd */
  82.     for (i = 0; i <= 7; i++) {
  83.     odd = 0;
  84.     temp = (unsigned int) *c;
  85.     /* ignore bit 0, lsb,  it will be parity (on vax) */
  86.     /* should do this with a table lookup */
  87.     for (j = 0; j <= 6; j++) {
  88.         temp = temp >> 1;
  89.         odd ^= temp & 01;
  90.     }
  91.     /* set odd parity in lsb */
  92.     if (!odd)
  93.         *c |= 1;
  94.     else
  95.         *c &= ~1;
  96.     c++;
  97.     }
  98.  
  99.     /* **** */
  100. #else
  101. #ifdef IBMPC
  102.  
  103.     srand(time());
  104.  
  105.     *k++ = rand();
  106.     *k = rand();
  107.  
  108.     /* make each byte parity odd */
  109.     for (i = 0; i <= 7; i++) {
  110.     odd = 0;
  111.     temp = (unsigned int) *c;
  112.     /* ignore bit 0, lsb,  it will be parity (on vax) */
  113.     /* should do this with a table lookup */
  114.     for (j = 0; j <= 6; j++) {
  115.         temp = temp >> 1;
  116.         odd ^= temp & 01;
  117.     }
  118.     /* set odd parity in lsb */
  119.     if (!odd)
  120.         *c |= 1;
  121.     else
  122.         *c &= ~1;
  123.     c++;
  124.     }
  125.  
  126. #else
  127.     /* **** */
  128.     dont know how to do random numbers for this machine;
  129. #endif /* IBMPC */
  130. #endif /* BSDUNIX */
  131.  
  132.     return 0;
  133. }
  134.